import { ITypeAdapterFactory } from './factory/ITypeAdapterFactory'
import { FIRST_READ } from './TypeConstants'

export class ReaderIn {
  private mReadValue: ESObject
  private mTypeFactory: ITypeAdapterFactory
  private mJson: ESObject
  private mTarget: ESObject
  private mDefaultTarget: ESObject | null = null
  private mReadNums: number = FIRST_READ

  constructor(
    json: ESObject, target: ESObject, factory: ITypeAdapterFactory,mReadNums: number
  ) {
    this.mTypeFactory = factory
    this.mJson = json
    this.mTarget = target
    this.mReadNums=mReadNums
  }

  setValue(value: ESObject) {
    this.mReadValue = value
  }

  value(): ESObject {
    if (this.mReadValue == undefined || this.mReadValue == null) {
      throw Error(
        "read value 可能是无效值，请检查read流程"
      )
    }
    return this.mReadValue
  }

  setDefault(defaultTarget: ESObject | null) {
    this.mDefaultTarget = defaultTarget
  }

  getDefault(): ESObject | null {
    return this.mDefaultTarget
  }

  getTarget(): ESObject {
    return this.mTarget
  }

  getJson(): ESObject {
    return this.mJson
  }


  getTypeFactory(): ITypeAdapterFactory {
    this.mReadNums += 1
    return this.mTypeFactory
  }

  getReadNums(): number {
    return this.mReadNums
  }
}